package brundfahrt

import org.springframework.dao.DataIntegrityViolationException

class SponsorshipController {
	def springSecurityService	
	
    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index() {
        redirect(action: "list", params: params)
    }

    def list() {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [sponsorshipInstanceList: Sponsorship.list(params), sponsorshipInstanceTotal: Sponsorship.count()]
    }

    def create() {
		def company = params.remove('company')
		def person = params.remove('person')
		def group = params.remove('group')
		def communicationPersonsInstance
		if (company?.id){
			params.put('sponsor.id', company.id)
			def companyInstance = Company.get(company.id)
			communicationPersonsInstance = new CommunicationController().getCommunicationPersonsInstance(companyInstance)		 
		} else if (person?.id){
			params.put('sponsor.id', person.id)
		} else if (group?.id){
			params.put('sponsor.id', company.id)
			def groupInstance = Group.get(group.id)
			communicationPersonsInstance = new CommunicationController().getCommunicationPersonsInstance(groupInstance)
			params.put('sponsor.id', group.id)
		}
		def test = new Person()
		if (isLoggedIn() && !params.containsKey('responsible.id')) {
			params.put('responsible.id', springSecurityService.getPrincipal().id)
		 }
        [sponsorshipInstance: new Sponsorship(params), communicationPersonsInstance: communicationPersonsInstance]
    }
	
    def save() {
        def sponsorshipInstance = new Sponsorship(params)
		sponsorshipInstance.communication = params.communication
        if (!sponsorshipInstance.save(flush: true)) {
			sponsorshipInstance.communication?.validate()
			def communicationPersonsInstance = new CommunicationController().getCommunicationPersonsInstance(sponsorshipInstance?.participant)
            render(view: "create", model: [sponsorshipInstance: sponsorshipInstance, communicationPersonsInstance: communicationPersonsInstance])
            return
        }

		flash.message = message(code: 'default.created.message', args: [message(code: 'sponsorship.label', default: 'Sponsorship'), sponsorshipInstance.id])
        redirect(action: "show", id: sponsorshipInstance.id)
    }

    def show() {
        def sponsorshipInstance = Sponsorship.get(params.id)
        if (!sponsorshipInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'sponsorship.label', default: 'Sponsorship'), params.id])
            redirect(action: "list")
            return
        }

        [sponsorshipInstance: sponsorshipInstance]
    }

    def edit() {
        def sponsorshipInstance = Sponsorship.get(params.id)
        if (!sponsorshipInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'sponsorship.label', default: 'Sponsorship'), params.id])
            redirect(action: "list")
            return
        }
		def communicationPersonsInstance = new CommunicationController().getCommunicationPersonsInstance(sponsorshipInstance?.participant)
        [sponsorshipInstance: sponsorshipInstance, communicationPersonsInstance: communicationPersonsInstance]
    }

    def update() {
        Sponsorship sponsorshipInstance = Sponsorship.get(params.id)
        if (!sponsorshipInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'sponsorship.label', default: 'Sponsorship'), params.id])
            redirect(action: "list")
            return
        }

        if (params.version) {
            def version = params.version.toLong()
            if (sponsorshipInstance.version > version) {
                sponsorshipInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'sponsorship.label', default: 'Sponsorship')] as Object[],
                          "Another user has updated this Sponsorship while you were editing")
				def communicationPersonsInstance = new CommunicationController().getCommunicationPersonsInstance(sponsorshipInstance?.participant)	
                render(view: "edit", model: [sponsorshipInstance: sponsorshipInstance, communicationPersonsInstance: communicationPersonsInstance])
                return
            }
        }
        sponsorshipInstance.properties = params
		sponsorshipInstance.communication = params.communication
        if (!sponsorshipInstance.save(flush: true)) {
			sponsorshipInstance.communication?.validate()
			def communicationPersonsInstance = new CommunicationController().getCommunicationPersonsInstance(sponsorshipInstance?.participant)
            render(view: "edit", model: [sponsorshipInstance: sponsorshipInstance, communicationPersonsInstance: communicationPersonsInstance])
            return
        }

		flash.message = message(code: 'default.updated.message', args: [message(code: 'sponsorship.label', default: 'Sponsorship'), sponsorshipInstance.id])
        redirect(action: "show", id: sponsorshipInstance.id)
    }
	
    def delete() {
        def sponsorshipInstance = Sponsorship.get(params.id)
        if (!sponsorshipInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'sponsorship.label', default: 'Sponsorship'), params.id])
            redirect(action: "list")
            return
        }

        try {
            sponsorshipInstance.delete(flush: true)
			flash.message = message(code: 'default.deleted.message', args: [message(code: 'sponsorship.label', default: 'Sponsorship'), params.id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
			flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'sponsorship.label', default: 'Sponsorship'), params.id])
            redirect(action: "show", id: params.id)
        }
    }
}
